package org.dromara.visitor.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.visitor.domain.dto.OrgManageAddReq;
import org.dromara.visitor.domain.dto.OrgManageImportExcel;
import org.dromara.visitor.domain.dto.OrgManageQueryReq;
import org.dromara.visitor.domain.dto.OrgManageResp;
import org.dromara.visitor.service.OrgManageService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
@RequestMapping("/orgManage")
public class OrgManageController {
    @Resource
    private OrgManageService orgManageService;

    /**
     * 新增、修改
     * @param addReq
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody OrgManageAddReq addReq){
        orgManageService.add(addReq);
        return R.ok();
    }

    /**
     * 分页列表
     * @param queryReq
     * @return
     */
    @PostMapping("/listPage")
    public R<Page<OrgManageResp>> listPage(@RequestBody OrgManageQueryReq queryReq){
        Page<OrgManageResp> page = orgManageService.listPage(queryReq);
        return R.ok(page);
    }

    /**
     * 导入
     * @param file
     * @return
     */
    @PostMapping("/import")
    public R importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        List<OrgManageImportExcel> list = ExcelUtil.importExcel(file.getInputStream(), OrgManageImportExcel.class);
        orgManageService.importExcel(list);
        return R.ok();
    }

    /**
     * 下载导入模板
     * @param response
     */
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) {
        String templatePath = "templates/vis_org_import.xlsx";
        // 下载文件名
        String fileName = "访客组织管理导入模板.xlsx";
        ExcelUtil.downloadTemplate(response, templatePath, fileName);
    }

    /**
     * 导出
     * @param queryReq
     * @param response
     */
    @PostMapping("/export")
    public void export(@RequestBody OrgManageQueryReq queryReq, HttpServletResponse response) {
        List<OrgManageResp> list = orgManageService.listExport(queryReq);
        ExcelUtil.exportExcel(list,"访客组织管理.xlsx" ,"访客组织管理", OrgManageResp.class, response);
    }

}
